package com.mmw.ruiji.controller;

import com.mmw.ruiji.entity.Employee;
import com.mmw.ruiji.entity.R;
import com.mmw.ruiji.server.EmployeeServer;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sound.midi.Soundbank;
import javax.swing.plaf.SpinnerUI;
import java.util.List;

/**
 * @author mamengwei
 */
@CrossOrigin
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeServer employeeServer;

    @PostMapping("/login")
    public R<String> login(@RequestBody Employee employee){
        System.out.println(employee);
        return R.success("登陆成功!");
    }

    @GetMapping("/page")
    public R<List<Employee>> getEmployees(){
        List<Employee> employees = employeeServer.getEmployees();
        for (Employee employee: employees){
            System.out.println(employee);
        }
        return R.success(employees);
    }

    @PostMapping("/add")
    public R<String> addEmployee(String username,String password,String name,String phone){
        System.out.println(username); System.out.println(password);
        System.out.println(name);
        System.out.println(phone);
        Employee employee = new Employee(username, name, password, phone);
        int i = employeeServer.addEmployee(employee);
        if (i==1){
            return R.success("添加成功!");
        }
        return R.error("添加失败!");
    }
    @PostMapping("/delete")
    public R<String> deleteEmployee(int id){

        System.out.println("id="+id);
        int i = employeeServer.deleteEmployee(id);
        if (i==1){
            return R.success("删除成功!");
        }
        return R.error("删除失败!");
    }


}
